<?php

class admin_ui_listing extends ui
{
    private $action = null;
    private $action_profix = null;
    private $actions = array();
    
    private $data = null;
    private $filter = null;
    private $fields = null;
    private $order_by = null;
    private $order_by_dir = null;
    
    private $footer = null;
    private $pagination = null;
    private $pagination_window = 15; // 控制分页最多显示多少个页码

    
    public function __construct()
    {
        $action = request('action');
        $this->action = $action;
        $this->action_prefix = substr($action, 0, strrpos($action, '.'));
        
        $this->actions['create'] = false;
        $this->actions['edit'] = false;
        $this->actions['block'] = false;
        $this->actions['unblock'] = false;
        $this->actions['delete'] = false;
    }

    public function can($command, $label = null, $action = null)
    {
        $new_label = '';
        $new_action = '';
        
        switch ($command)
        {
            case 'create':
                $new_label = ($label == null) ? '新建' : $label;
                $new_action = ($action == null) ? ($this->action_prefix . '.edit') : $action;
                break;
            case 'edit':
                $new_label = ($label == null) ? '编辑' : $label;
                $new_action = ($action == null) ? ($this->action_prefix . '.edit') : $action;
                break;
            case 'block':
                $new_label = ($label == null) ? '屏蔽' : $label;
                $new_action = ($action == null) ? ($this->action_prefix . '.block') : $action;
                break;
            case 'unblock':
                $new_label = ($label == null) ? '公开' : $label;
                $new_action = ($action == null) ? ($this->action_prefix . '.unblock') : $action;
                break;
            case 'delete':
                $new_label = ($label == null) ? '删除' : $label;
                $new_action = ($action == null) ? ($this->action_prefix . '.delete') : $action;
                break;
        }
        
        $this->actions[$command] = array('label'=>$new_label, 'action'=>$new_action);
    }

    public function set_action($type, $action)
    {
        $this->actions[$type] = $action;
    }

    public function set_data($objs)
    {
        $this->data = $objs;
    }

    public function set_filter()
    {
        $this->filter = func_get_args();
    }

    public function set_fields()
    {
        $this->fields = func_get_args();
    }

    public function set_footer($footer)
    {
        $this->footer = $footer;
    }

    public function order_by($order_by, $order_by_dir)
    {
        $this->order_by = $order_by;
        $this->order_by_dir = $order_by_dir;
    }

    public function set_pagination($pagination)
    {
        $this->pagination = $pagination;
    }

    public function set_pagination_window($pagination_window)
    {
        $this->pagination_window = $pagination_window;
    }

    public function display()
    {
        $n = count($this->fields);
        if ($n == 0) return;
        
        $checkbox = false;
        if ($this->actions['edit'] != false || $this->actions['block'] != false || $this->actions['unblock'] != false || $this->actions['delete'] != false) $checkbox = true;
        
        if ($checkbox) $n++;
        if ($this->actions['edit'] != false) $n++;
        if ($this->actions['block'] != false || $this->actions['unblock'] != false) $n++;
        if ($this->actions['delete'] != false) $n++;

        echo '<link type="text/css" rel="stylesheet" href="uis/listing/css/listing.css" />';
        echo '<script type="text/javascript" language="javascript" src="uis/listing/js/listing.js"></script>';
        echo '<script type="text/javascript" language="javascript">';
        echo 'admin_ui_listing.setAction("' . $this->action . '");';
        foreach($this->actions as $key=>$val)
        {
            if($val!=false) echo 'admin_ui_listing.setActions("' . $key . '", "'.$val['action'].'");';
        }
        echo '</script>';
        
        echo '<div class="admin_ui_listing">';
        echo '<form action="./" id="admin_ui_listing_form" method="get">';
        echo '<div class="toolbar">';
        
        echo '<table>';
        echo '<tr>';
        echo '<td align="left" valign="bottom"><div class="filter">';
        
        if ($this->filter !== null)
        {
            foreach ($this->filter as $filter)
            {
                echo $filter['label'] . ': ';
                switch ($filter['type'])
                {
                    case 'text':
                        if (isset($filter['html']))
                            echo $filter['html'];
                        else
                        {
                            echo '<input type="text" name="' . $filter['name'] . '" id="' . $filter['name'] . '"';
                            if (isset($filter['value'])) echo ' value="' . $filter['value'] . '"';
                            if (isset($filter['width'])) echo ' style="width:' . $filter['width'] . ';"';
                            echo ' />';
                        }
                        break;
                    case 'radio':
                        if (isset($filter['html']))
                            echo $filter['html'];
                        else
                        {
                            if (isset($filter['options']) && count($filter['options']))
                            {
                                foreach ($filter['options'] as $key => $val)
                                {
                                    echo '<input type="radio" name="' . $filter['name'] . '" id="' . $filter['name'] . '-' . $key . '" value="' . $filter['value'] . '"';
                                    if (isset($filter['value']) && $filter['value'] == $key) echo ' checked="checked"';
                                    echo ' />';
                                    echo '<label for="' . $filter['name'] . '-' . $key . '">' . $val . '</label>';
                                    echo '&nbsp;';
                                }
                            }
                        }
                        break;
                    case 'checkbox':
                        if (isset($filter['html']))
                            echo $filter['html'];
                        else
                        {
                            if (isset($filter['options']) && count($filter['options']))
                            {
                                $name = (count($filter['options']) > 1) ? ($filter['name'] . '[]') : $filter['name'];
                                foreach ($filter['options'] as $key => $val)
                                {
                                    echo '<input type="checkbox" name="' . $name . '" id="' . $filter['name'] . '-' . $key . '" value="' . $filter['value'] . '"';
                                    if (isset($filter['value']) && $filter['value'] == $key) echo ' checked="checked"';
                                    echo ' />';
                                    echo '<label for="' . $filter['name'] . '-' . $key . '">' . $val . '</label>';
                                    echo '&nbsp;';
                                }
                            }
                        }
                        break;
                    case 'select':
                        if (isset($filter['html']))
                            echo $filter['html'];
                        else
                        {
                            echo '<select name="' . $filter['name'] . '" id="' . $filter['name'] . '">';
                            if (isset($filter['options']) && count($filter['options']))
                            {
                                foreach ($filter['options'] as $key => $val)
                                {
                                    echo '<option value="' . $key . '"';
                                    if (isset($filter['value']) && $filter['value'] == $key) echo ' selected="selected"';
                                    echo '>' . $val . '</option>';
                                }
                            }
                            echo '</select>';
                        }
                        break;
                }
                echo ' &nbsp; ';
            }
            echo '<input type="submit" value="查找" >';
        }
        echo '</div></td>';
        
        if ($this->actions['create'] != false) echo '<td width="46" align="center"><a class="icon create able" id="admin_ui_listing_toolbar_create" href="javascript:;" onclick="javascript:admin_ui_listing.create();" title="' . $this->actions['create']['label'] . '">' . $this->actions['create']['label'] . '</a></td>';
        if ($this->actions['edit'] != false) echo '<td width="46" align="center"><a class="icon edit disable" id="admin_ui_listing_toolbar_edit" href="javascript:;" onclick="javascript:if(!$(this).hasClass(\'disable\')){admin_ui_listing.edit(0);}" title="' . $this->actions['edit']['label'] . '选中项">' . $this->actions['edit']['label'] . '</a></td>';
        if ($this->actions['unblock'] != false) echo '<td width="46" align="center"><a class="icon unblock disable" id="admin_ui_listing_toolbar_unblock" href="javascript:;" onclick="javascript:if(!$(this).hasClass(\'disable\')){admin_ui_listing.unblock(0);}" title="' . $this->actions['unblock']['label'] . '选中项">' . $this->actions['unblock']['label'] . '</a></td>';
        if ($this->actions['block'] != false) echo '<td width="46" align="center"><a class="icon block disable" id="admin_ui_listing_toolbar_block" href="javascript:;" onclick="javascript:if(!$(this).hasClass(\'disable\')){admin_ui_listing.block(0);}" title="' . $this->actions['block']['label'] . '选中项">' . $this->actions['block']['label'] . '</a></td>';
        if ($this->actions['delete'] != false) echo '<td width="46" align="center"><a class="icon delete disable" id="admin_ui_listing_toolbar_delete" href="javascript:;" onclick="javascript:if(!$(this).hasClass(\'disable\')){admin_ui_listing.remove(0);}" title="' . $this->actions['delete']['label'] . '选中项">' . $this->actions['delete']['label'] . '</a></td>';
        
        echo '</tr>';
        echo '</table>';
        
        echo '</div>';
        
        echo '<div class="listing">';
        
        echo '<table>';
        echo '<thead>';
        echo '<tr>';
        
        if ($checkbox) echo '<th align="center" width="20"><input type="checkbox" id="admin_ui_listing_check_all" /></th>';
        foreach ($this->fields as $field)
        {
            echo '<th align="' . (isset($field['align']) ? $field['align'] : 'center') . '"' . (isset($field['width']) ? ('width=' . intval($field['width'])) : '') . '>';
            if (isset($field['order_by']))
            {
                $order_by_dir = 'ASC';
                if ($this->order_by == $field['order_by'] && $this->order_by_dir == 'ASC') $order_by_dir = 'DESC';
                
                echo '<a href="javascript:;" onclick="javascript:admin_ui_listing.orderBy(\'' . $field['order_by'] . '\', \'' . $order_by_dir . '\');">';
                echo isset($field['label']) ? $field['label'] : '';
                if ($this->order_by == $field['order_by']) echo ($this->order_by_dir == 'ASC') ? ' &darr;' : ' &uarr;';
                echo '</a>';
            }
            else
                echo isset($field['label']) ? $field['label'] : '';
            echo '</th>';
        }
        
        if ($this->actions['block'] != false || $this->actions['unblock'] != false)
        {
            echo '<th align="center" width="80">';
            if ($this->actions['unblock'] != false) echo $this->actions['unblock']['label'];
            if ($this->actions['block'] != false && $this->actions['unblock'] != false) echo '/';
            if ($this->actions['block'] != false) echo $this->actions['block']['label'];
            echo '</th>';
        }
        if ($this->actions['edit'] != false) echo '<th align="center" width="40">' . $this->actions['edit']['label'] . '</th>';
        if ($this->actions['delete'] != false) echo '<th align="center" width="40">' . $this->actions['delete']['label'] . '</th>';
        
        echo '</tr>';
        echo '</thead>';
        
        echo '<tbody>';
        if (count($this->data))
        {
            foreach ($this->data as $obj)
            {
                echo '<tr id="admin_ui_listing_row_' . $obj->id . '" class="row">';
                
                if ($checkbox) echo '<td align="center" width="20"><input type="checkbox" class="id" value="' . $obj->id . '" /></td>';
                foreach ($this->fields as $field)
                {
                    echo '<td align="' . (isset($field['align']) ? $field['align'] : 'center') . '"' . (isset($field['width']) ? ('width=' . intval($field['width'])) : '') . '>';
                    
                    if (isset($field['template']))
                    {
                        $str = $field['template'];
                        $start = strpos($str, '{');
                        while ($start !== false)
                        {
                            $end = strpos($str, '}');
                            $key = substr($str, $start + 1, ($end - $start - 1));
                            $val = isset($obj->$key) ? $obj->$key : '';
                            $str = str_replace('{' . $key . '}', $val, $str);
                            $start = strpos($str, '{');
                        }
                        echo $str;
                    }
                    else
                    {
                        echo $obj->{$field['name']};
                    }
                    
                    echo '</td>';
                }
                
                if ($this->actions['block'] != false || $this->actions['unblock'] != false)
                {
                    echo '<td align="center" width="60">';
                    if ($obj->block == 0)
                    {
                        if ($this->actions['block'] != false) echo '<a href="javascript:;" onclick="javascript:admin_ui_listing.block(\'' . $obj->id . '\');" class="icon unblock" title="点击' . $this->actions['block']['label'] . '"></a>';
                    }
                    else
                    {
                        if ($this->actions['unblock'] != false) echo '<a href="javascript:;" onclick="javascript:admin_ui_listing.unblock(\'' . $obj->id . '\');" class="icon block" title="点击' . $this->actions['unblock']['label'] . '"></a>';
                    }
                    echo '</td>';
                }
                
                if ($this->actions['edit'] != false) echo '<td align="center" width="40"><a href="javascript:;" onclick="javascript:admin_ui_listing.edit(\'' . $obj->id . '\');" class="icon edit" title="' . $this->actions['edit']['label'] . '"></a></td>';
                if ($this->actions['delete'] != false) echo '<td align="center" width="40"><a href="javascript:;" onclick="javascript:admin_ui_listing.remove(\'' . $obj->id . '\');" class="icon delete" title="' . $this->actions['delete']['label'] . '"></a></td>';
                echo '</tr>';
            }
        }
        else
        {
            echo '<tr>';
            echo '<td colspan="' . $n . '" align="center">没有记录</td>';
            echo '<tr>';
        }
        echo '</tbody>';
        
        $total = $pages = $limit = $page = 0;
        if ($this->pagination !== null)
        {
            $total = $this->pagination->get_total();
            $pages = $this->pagination->get_pages();
            $limit = $this->pagination->get_limit();
            $page = $this->pagination->get_page();
        }
        
        echo '<tfoot>';
        echo '<tr>';
        echo '<td colspan="' . $n . '">';
        if ($this->footer === null)
        {
            if ($this->pagination !== null)
            {
                echo '总计 <strong>' . $total . '</strong> 条记录';
                if ($pages > 1) echo '( <strong>' . $pages . '</strong> 页)，每页显示 <strong>' . $limit . '</strong> 条';
            }
            else
            {
                echo '&nbsp;';
            }
        }
        else
        {
            echo $this->footer;
        }
        echo '</td>';
        echo '</tr>';
        echo '</tfoot>';
        
        echo '</table>';
        echo '</div>';
        
        if ($this->pagination !== null && $pages > 1)
        {
            $window = $this->pagination_window;
            $start_page = $page - intval($window / 2);
            if ($start_page < 1) $start_page = 1;
            $end_page = $start_page + $window - 1;
            if ($end_page > $pages) $end_page = $pages;
            
            echo '<div class="pagination">';
            echo '<ul>';
            if ($page == 1)
            {
                echo '<li class="prev disabled"><a href="#">&larr; 上一页</a></li>';
            }
            else
            {
                echo '<li class="prev"><a href="javascript:" onclick="javascript:admin_ui_listing.gotoPage(' . ($page - 1) . ');">&larr; 上一页</a></li>';
            }
            for ($i = $start_page; $i <= $end_page; $i++)
            {
                if ($i == $page)
                {
                    echo '<li class="active"><a href="#">' . $i . '</a></li>';
                }
                else
                {
                    echo '<li><a href="javascript:" onclick="javascript:admin_ui_listing.gotoPage(' . $i . ');">' . $i . '</a></li>';
                }
            }
            if ($page < $pages)
            {
                echo '<li class="next"><a href="javascript:" onclick="javascript:admin_ui_listing.gotoPage(' . ($page + 1) . ');">下一页  &rarr;</a></li>';
            }
            else
            {
                echo '<li class="next disabled"><a href="#">下一页  &rarr;</a></li>';
            }
            echo '</ul>';
            echo '</div>';
        }
        
        echo '<input type="hidden" id="admin_ui_listing_id" name="id" value="">';
        echo '<input type="hidden" id="admin_ui_listing_page" name="page" value="' . $page . '">';
        if ($this->order_by !== null)
        {
            echo '<input type="hidden" id="admin_ui_listing_order_by" name="order_by" value="' . $this->order_by . '">';
            echo '<input type="hidden" id="admin_ui_listing_order_by_dir" name="order_by_dir" value="' . $this->order_by_dir . '">';
        }
        echo '<input type="hidden" id="admin_ui_listing_action" name="action" value="' . $this->action . '">';
        
        echo '</form>';
        echo '</div>';
    
    }

}
?>